
//
//  EWMJDIYHeader.m
//  haveSomethingToEat
//
//  Created by 陈宁权 on 2017/4/9.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWMJDIYHeader.h"

@interface EWMJDIYHeader()
@property (weak, nonatomic) UILabel *label;
//@property (weak, nonatomic) UIImageView *logo;
@property (weak, nonatomic) UILabel *lastUpdateTime;
@end

@implementation EWMJDIYHeader

- (void)prepare
{
    [super prepare];
    
    self.mj_h = kScreenH;
    
    self.backgroundColor = [UIColor colorWithRed:133/255.0 green:32/255.0 blue:133/255.0 alpha:1];
    
    //label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    // loading
    UILabel *lastUpdateTime = [[UILabel alloc] init];
    NSDate *lastUpdatedTime = [[NSUserDefaults standardUserDefaults] objectForKey:self.lastUpdatedTimeKey];
    
    // 1.获得年月日
    NSCalendar *calendar = [self currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    BOOL isToday = NO;
    if ([cmp1 day] == [cmp2 day]) { // 今天
        formatter.dateFormat = @" HH:mm";
        isToday = YES;
    } else if ([cmp1 year] == [cmp2 year]) { // 今年
        formatter.dateFormat = @"MM-dd HH:mm";
    } else {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    NSString *time = [formatter stringFromDate:lastUpdatedTime];
    
    // 3.显示日期
    lastUpdateTime.text = [NSString stringWithFormat:@"%@%@%@",
                                      [NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
                                      isToday ? [NSBundle mj_localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
                                      time];
    lastUpdateTime.font = [UIFont systemFontOfSize:12];
    lastUpdateTime.textColor = [UIColor whiteColor];
    lastUpdateTime.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:lastUpdateTime];
    self.lastUpdateTime = lastUpdateTime;
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-25);
        make.centerX.equalTo(self);
        make.width.equalTo(self);
        make.height.offset(10);
    }];
    
    [self.lastUpdateTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.centerX.equalTo(self);
        make.width.equalTo(self);
        make.height.offset(15);
    }];
}

- (NSCalendar *)currentCalendar {
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    return [NSCalendar currentCalendar];
}


- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            self.label.text = @"下拉可以刷新";
            break;
        case MJRefreshStatePulling:
            self.label.text = @"松开立即刷新";
            break;
        case MJRefreshStateRefreshing:
            self.label.text = @"正在刷新数据...";
            // 保存刷新时间
            [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:self.lastUpdatedTimeKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        default:
            break;
    }
}

- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];

    self.label.textColor = [UIColor whiteColor];
    
    NSDate *lastUpdatedTime = [[NSUserDefaults standardUserDefaults] objectForKey:self.lastUpdatedTimeKey];
    
    // 1.获得年月日
    NSCalendar *calendar = [self currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    BOOL isToday = NO;
    if ([cmp1 day] == [cmp2 day]) { // 今天
        formatter.dateFormat = @" HH:mm";
        isToday = YES;
    } else if ([cmp1 year] == [cmp2 year]) { // 今年
        formatter.dateFormat = @"MM-dd HH:mm";
    } else {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    NSString *time = [formatter stringFromDate:lastUpdatedTime];
    self.lastUpdateTime.text = [NSString stringWithFormat:@"%@%@%@",
                                [NSBundle mj_localizedStringForKey:MJRefreshHeaderLastTimeText],
                                isToday ? [NSBundle mj_localizedStringForKey:MJRefreshHeaderDateTodayText] : @"",
                                time];
}


@end
